<?php
namespace Admin\Controller;
use Think\Controller;

class PublicController extends Controller{
	/**
    *author wangdong
    *date 6-23
    *action 后台login 方法
    **/
	public function login(){

		if(IS_POST){
			//拿到数据
			$data['username'] = I('post.username');
    		$data['password'] = I('post.password');

    		$model = M('admin_user');	

    		$res = $model->where(array('username'=>$data['username']))->find();
    		// 判断是否存在该用户
    		if(!$res){
    			$this->error('该用户不存在,请先注册');
    		}else{
    			// 判断密码
	    		if($res['password'] !==$data['password']){
	    			$this->error('密码有误');
	    		}
	    		$_SESSION['admin_user_info'] = $res;

                //权限管理
                $node_lists = M('role')->field('powers')->where(array('id' => $_SESSION['admin_user_info']['id']))->find();
                $arr = explode(',',$node_lists['powers']);

                $date =array();
                foreach ($arr as $value) {
                   $date[] =M('role_node')->query("select * from tj_role_node where id =".$value);
                }
                
                $node_list =array();
                foreach ($date as $key => $vo) {
                    foreach ($vo as $k => $v) {
                       $node_list[]=$v;
                    }
                }

                $node_lists = array();
                foreach ($node_list as $key => $vo) {
                   $node_lists[]= $vo['controller_name'].'_'.$vo['action_name'];
                }
                
                $_SESSION['admin_user_info']['node_lists'] = $node_lists;

	    		$this->success('登录成功',U('Index/index'),2);
			}


		}else{
			$this->display();
		}
		
	}
	/**
    *author wangdong
    *date 6-23
    *action 后台reigster 注册方法
    **/
   public function register()
    {
    	if(IS_POST){
    		$data  = I('post.');

    		$model = D('admin_user');

    		if($model->create()){
    			if($model->add()){
    				$this->success('注册成功！','login');
    			}
    		}else{
				$this->error($model->getError());
			}
    	}else{
    		$this->display();
    	}
    }
    /**
    *author wangdong
    *date 6-23
    *action admin logout 方法
    **/
    public function logout()
	{
		unset($_SESSION['admin_user_info']);
		$this->redirect('Index/index');
		exit;
	}
    /**
    *author wangdong
    *date 6-26
    *action demo 方法
    **/
    public function demo(){
        dump($_SESSION['admin_user_info']);


        
        // $node_lists = M('role')->field('powers')->where(array('id' => $_SESSION['admin_user_info']['id']))->find();
        // $arr = explode(',',$node_lists['powers']);

        // $date =array();
        // foreach ($arr as $value) {
        //    $date[] =M('role_node')->query("select * from tj_role_node where id =".$value);
        // }
        
        // $node_list =array();
        // foreach ($date as $key => $vo) {
        //     foreach ($vo as $k => $v) {
        //        $node_list[]=$v;
        //     }
        // }

        // $node_lists = array();
        // foreach ($node_list as $key => $vo) {
        //    $node_lists[]= $vo['controller_name'].'_'.$vo['action_name'];
        // }
        
        // $_SESSION['admin_user_info']['node_lists'] = $node_lists;


        // dump($_SESSION['admin_user_info']);
        
    }
}